package com.orhon.smartcampus.modules.material.mutation;

import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.alibaba.fastjson.JSON;
import com.orhon.smartcampus.auth.JwtUtils;
import com.orhon.smartcampus.basicComponents.Result;
import com.orhon.smartcampus.modules.base.dao.SchoolsDao;
import com.orhon.smartcampus.modules.material.dao.BuildingsDao;
import com.orhon.smartcampus.modules.material.dao.CampusDao;
import com.orhon.smartcampus.modules.material.dao.SiteDao;
import com.orhon.smartcampus.modules.material.entity.Buildings;
import com.orhon.smartcampus.modules.material.entity.Site;
import com.orhon.smartcampus.modules.material.gqlInputModel.GQLBuildingInput;
import com.orhon.smartcampus.modules.material.inputMapper.BuildingInputMapper;
import com.orhon.smartcampus.modules.user.dao.TokenService;

import graphql.schema.DataFetcher;
import graphql.schema.DataFetchingEnvironment;

@Component
public class AddBuildingMutation implements DataFetcher {


    @Autowired
    BuildingsDao buildingsDao;

    @Autowired
    CampusDao campusDao;

    @Autowired
    SchoolsDao schoolsDao;

    @Autowired
    SiteDao siteDao;

    @Autowired
    TokenService tokenService;

    /**
     * 建筑管理
     * editor : ths
     */
    @Override
    public Object get(DataFetchingEnvironment environment) throws Exception {

        //String token = (String) SecurityUtils.getSubject().getPrincipal();
        String token = tokenService.getSystemToken();
        Long schoolId = JwtUtils.getSchoolId(token);

        GQLBuildingInput input = BuildingInputMapper.MAPPER.mapInput(environment.getArgument("inputData"));

        Buildings buildings = null;

        if (input.getId() != null) {
            buildings = buildingsDao.get(Buildings.class, input.getId());
            buildings.setUpdatedAt(new Date());
        } else {
            buildings = new Buildings();
            buildings.setCreatedAt(new Date());
        }
        if(input.getCode() != null){

            buildings.setBuildingsCode(input.getCode());
        }
        if (input.getCampus_id() != null) {
//            buildings.setMaterialCampusByCampusId(campusDao.get(Campus.class, input.getCampus_id()));
        }
        if (input.getSite_id() != null) {
            buildings.setSite(siteDao.get(Site.class, input.getSite_id()));
        }
        if (schoolId != null) {
//            buildings.setBaseSchoolsBySchoolId(schoolsDao.get(Schools.class, schoolId));
        }
        if(input.getUnit_num() != null){

            buildings.setUnitNum(input.getUnit_num());
        }
        if(input.getFloor_num() != null){

            buildings.setFloorNum(input.getFloor_num());
        }
        if(input.getStatus() != null){

            buildings.setBuildingsStatus(input.getStatus());
        }
        if(input.getBuild_at() != null){

            buildings.setBuildingsAt(input.getBuild_at());
        }
        if(input.getYears() != null){

            buildings.setBuildingsYears(input.getYears());
        }
        if(input.getTel() != null){

            buildings.setBuildingsTel(input.getTel());
        }
        if(input.getUse() != null){

            buildings.setBuildingsUse(input.getUse());
        }
        if(input.getCost() != null){

            buildings.setBuildingsCost(input.getCost());
        }
        if(input.getArea() != null){

            buildings.setBuildingsArea(input.getArea());
        }
        if(input.getOccupied_area() != null){

            buildings.setBuildingsOnarea(input.getOccupied_area());
        }
        if(input.getName() != null){

            buildings.setBuildingsName(JSON.toJSONString(input.getName()));
        }
        if(input.getProperty() != null){

            buildings.setBuildingsProperty(JSON.toJSONString(input.getProperty()));
        }
        if(input.getMaterial() != null){

            buildings.setBuildingsMaterial(JSON.toJSONString(input.getMaterial()));
        }
        if(input.getIntro() != null){

            buildings.setBulidingsIntroduction(JSON.toJSONString(input.getIntro()));
        }
        try {
            buildingsDao.saveOrUpdate(buildings);
        } catch (Exception e) {
            return Result.error(500, "" + e);
        }
        return Result.ok();
    }
}

